import datetime
import threading
from jili.price import tick
from jili .price.getdata import gettick
import copy
class active():
    def __init__(self,config={}):
        self.config=copy.deepcopy(config)
        self.arg=["oncall"]
        self.initarg()
        #print(self,"active init:",id(config),id(self),id(self.config),id(self.arg))
    def initarg(self):
        pass
    def getconfigarg(self):
        return self.arg
    def setconfig(self,config):
        self.config.update(config)
        #print("active getconfigarg:",id(self.config))
    def checkarg(self):
        ret=True
        #print("active checkarg:",id(self.arg),id(self.config))
        for i in self.arg:
            if i not in self.config.keys():
                ret=False
        return ret
    def initclac(self):
        pass
    def start(self,config={}):
        if config:
            self.config.update(config)
        #print("active config",id(config),id(self.config),":",self.config)
        if self.checkarg():
            self.initclac()
            if "obj" in self.config.keys():
                tick.subtick(self.ontick,"tick",self.config["obj"])
        else:
            print("active arg err: must need:",self.arg,"now:",self.config)
    def stop(self):
        if self.checkarg():
            if "obj" in self.config.keys():
                tick.unsubtick(self.config["obj"],oncall=self.ontick)
    def ontick(self,t):
        pass
"""
1.定时触发:active_bytime
宏观
2.定价触发:active_byprice  buy or sell
3.移动gap触发
3.到百分比触发:active_byprecent  buy or sell
微观
4.不涨撤1tick触发:active_bytick_switch  buy or sell
5.多长时间不涨触发active_bytime_switch  buy or sell
6.多少个变动价不涨触发:active_bycount_switch  buy or sell
7.时积值触发:active_bytime_acc  buy or sell
8.点积值触发:active_bycount_acc  buy or sell
"""
class active_bytime(active):
    def initarg(self):
        self.arg=["time","oncall"]
    def start(self,config={}):
        if config:
            self.config.update(config)
        if "time" in self.config.keys():
            if "oncall" in self.config.keys():
                t=self.config["time"]
                self.timer = threading.Timer(t,self.config["oncall"])
                self.timer.start()
    def stop(self):
        self.timer.cancel()
class active_byprice_buy(active):
    def initarg(self):
        self.arg=["obj","anchor","oncall"]
    def ontick(self,t):
        lastprice=t["LastPrice"]
        if lastprice<=self.config["anchor"]:
            self.config["oncall"](t)
class active_byprice_sell(active):
    def __init__(self,config={}):
        super(active_byprice_sell, self).__init__(config)
        self.arg=["obj","anchor","oncall"]
    def ontick(self,t):
        lastprice=t["LastPrice"]
        if lastprice>=self.config["anchor"]:
            self.config["oncall"](t)
class active_bymovegap_buy(active):
    def __init__(self,config={}):
        super(active_bymovegap_buy, self).__init__(config)
        self.arg=["obj","anchor","oncall"]
        self.anchor=None
    def ontick(self,t):
        lastprice=t["LastPrice"]
        if self.anchor:
            metre=self.anchor+self.config["gap"]
            if lastprice>=metre:
                self.config["oncall"](t)
        else:
            self.anchor=lastprice
class active_bymovegap_sell(active):
    def __init__(self,config={}):
        super(active_bymovegap_sell, self).__init__(config)
        self.arg=["obj","anchor","oncall"]
        self.anchor=None
    def ontick(self,t):
        lastprice=t["LastPrice"]
        if self.anchor:
            metre=self.anchor-self.config["gap"]
            if lastprice<=metre:
                self.config["oncall"](t)
        else:
            self.anchor=lastprice
class active_bypercent_buy(active):
    def __init__(self,config={}):
        super(active_bypercent_buy, self).__init__(config)
        self.arg=["obj","anchor","percent","oncall"]
    def initclac(self):
        self.metre=self.config["anchor"]*(1-self.config["percent"])
    def ontick(self,t):
        lastprice=t["LastPrice"]
        if lastprice<=self.metre:
            self.config["oncall"](t)
class active_bypercent_sell(active):
    def __init__(self,config={}):
        super(active_bypercent_sell, self).__init__(config)
        self.arg=["obj","anchor","percent","oncall"]
    def initclac(self):
        self.metre=self.config["anchor"]*(1+self.config["percent"])
    def ontick(self,t):
        lastprice=t["LastPrice"]
        if lastprice>=self.metre:
            self.config["oncall"](t)
